package golang

import (
	"gitee.com/allowall-package/goutil/fn"
)

// 选择排序
func SelectionSort(a []int) []int {
	n := len(a)

	if n < 1 {
		return a
	}

	for i := 0; i < n-1; i++ {
		// 每轮需要找到这一轮最小元素的索引
		k := i
		for j := i + 1; j < n; j++ {
			if a[j] < a[k] {
				k = j
			}
		}

		if i != k {
			a[i], a[k] = a[k], a[i]
		}
	}

	return a
}

func SelectionSortTest() {
	fn.SliceRand(a)
	s := make([]int, len(a))
	copy(s, a)

	fn.Dump(fn.FuncInfo(), s, SelectionSort(a))
}
